package com.tender;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EasyTestAdvance4ExclusivegatewayAPI {

    /**
     * 流程部署
     */
    @Test
    public void test01() {
        // 1.获取ProcessEngine对象
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取RepositoryService进行部署操作
        RepositoryService service = engine.getRepositoryService();
        // 3.使用RepositoryService进行部署操作
        Deployment deploy = service.createDeployment()
                .addClasspathResource("bpmn/tender_evection_exclusivegateway.bpmn") // 添加bpmn资 源
                .addClasspathResource("bpmn/tender_evection_exclusivegateway.png") // 添加png资源
                .name("出差申请流程-exclusivegateway")
                .deploy();// 部署流程
        // 4.输出流程部署的信息
        System.out.println("流程部署的id:" + deploy.getId());
        System.out.println("流程部署的名称：" + deploy.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void test02() {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = engine.getRuntimeService();
        // 流程定义key
        // 查看表：act_re_procdef 的 KEY_ 字段获得
        String key = "tender_evection_ExclusiveGateway";
        // 启动实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key);
        // 输出信息
        System.out.println("获取流程实例名称：" + processInstance.getName());
        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());

        /**
         * 获取流程实例名称：null
         * 流程定义ID：tender_evection_ExclusiveGateway:1:60004
         */
    }

    /**
     * 根据负责人查询 任务
     */
    @Test
    public void test03() {
        String key = "tender_evection_ExclusiveGateway";
        String assignee = "creater_tender";
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = engine.getTaskService();
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey(key)
                //.taskCandidateUser(candidateUser)
                //.taskCandidateOrAssigned(candidateUser)
                .taskAssignee(assignee)
                .list();
        for (Task task : list) {
            System.out.println("流程实例Id：" + task.getProcessInstanceId());
            System.out.println("任务ID：" + task.getId());
            System.out.println("负责人:" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        }
        /**
         * 流程实例Id：62501
         * 任务ID：62505
         * 负责人:creater_tender
         * 任务名称：创建出差申请单
         */
    }

    /**
     * 完成个人任务
     */
    @Test
    public void test04() {
        String taskId = "67503";
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        Map<String,Object> map = new HashMap<>();
        Evection evection = new Evection();
        evection.setNum(2d);
        map.put("evection", evection);

        TaskService taskService = engine.getTaskService();
        taskService.complete(taskId, map);
        System.out.println("完成任务：" + taskId);
    }

    /**
     * 归还任务
     */
    @Test
    public void test07() {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = engine.getTaskService();
        String taskId = "52502";
        String userId = "manager_tender";
        Task task = taskService.createTaskQuery()
                .taskId(taskId)
                .taskAssignee(userId)
                .singleResult();
        if (task != null) {
            // 如果设置为null，归还组任务，任务没有负责人
            taskService.setAssignee(taskId, null);
        }
    }

    /**
     * 任务交接
     */
    @Test
    public void test08() {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = engine.getTaskService();
        String taskId = "52502";
        String userId = "manager_tender";
        Task task = taskService.createTaskQuery()
                .taskId(taskId)
                .taskAssignee(userId)
                .singleResult();
        if (task != null) {
            // 设置该任务的新的负责人
            taskService.setAssignee(taskId, "general_tender");
        }
    }

}
